package com.game.springbootstart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.game.springbootstart.DTO.CommentDTO;
import com.game.springbootstart.DTO.CommentsDTO;
import com.game.springbootstart.VO.CommentVO;
import com.game.springbootstart.VO.CommentsVO;
import com.game.springbootstart.entity.CommentLikes;
import com.game.springbootstart.mapper.CommentLikesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.game.springbootstart.entity.Comments;
import com.game.springbootstart.mapper.CommentsMapper;
import com.game.springbootstart.service.ICommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.stream.events.Comment;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-17
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements ICommentsService {
    @Autowired
    private com.game.springbootstart.mapper.CommentsMapper commentsMapper;
    @Autowired
    private CommentLikesMapper commentLikesMapper;


    @Transactional
    public boolean updateBycommentId(Long commentId, Boolean like, Long userId) {
        if (commentId == null || userId == null || like == null) {
            System.out.println("未接收到参数");
            return false;
        }

        // 检查当前用户是否已点赞
        boolean hasLiked = commentLikesMapper.checkIfLiked(commentId, userId) > 0;

        // 如果当前状态与目标状态一致，无需操作
        if (hasLiked == like) {
            return true;
        }

        UpdateWrapper<Comments> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", commentId);

        if (like) {
            // 点赞：添加记录并增加 like_count
            CommentLikes commentLike = new CommentLikes();
            commentLike.setCommentId(commentId);
            commentLike.setUserId(userId);
            commentLike.setCreatedAt(LocalDateTime.now());
            commentLikesMapper.insert(commentLike);
            updateWrapper.setSql("like_count = like_count + 1");
        } else {
            // 取消点赞：删除记录并减少 like_count
            commentLikesMapper.delete(new QueryWrapper<CommentLikes>()
                    .eq("comment_id", commentId)
                    .eq("user_id", userId));
            updateWrapper.setSql("like_count = like_count - 1");
        }

        // 更新 comments 表的 like_count
        int updated = commentsMapper.update(null, updateWrapper);
        return updated > 0;
    }

    // 检查是否已点赞
    public boolean isLiked(Long commentId, Long userId) {
        return commentLikesMapper.checkIfLiked(commentId, userId) > 0;
    }

    @Override
    public List<CommentsDTO> getCommentsByGameId(CommentsVO commentsVO) {
        return commentsMapper.getCommentsByGameId(commentsVO);
    }

    @Override
    public Boolean delete(CommentsVO commentsVO) {
        return commentsMapper.deleteBy(commentsVO);
    }

    @Override
    public List<CommentsDTO> getCommentsByUserId(Long userId) {
        return commentsMapper.getCommentsByUserId(userId);
    }

    @Override
    public void removeByParentId(Long commentId) {
        commentsMapper.removeByParentId(commentId);
    }

    @Override
    public void removeByMainId(Long commentId) {
        commentsMapper.removeByMainId(commentId);
    }

    @Override
    public List<CommentsDTO> pageComments(CommentVO commentVO) {
        return List.of();
    }

    @Override
    public String getName(Long userId) {
        return commentsMapper.getName(userId);
    }

    @Override
    public String getGame(Long gameId) {
        return commentsMapper.getGame(gameId);
    }

    @Override
    public List<CommentDTO> getComments(CommentVO commentVO, int startRow) {
        return commentsMapper.getComments(commentVO,startRow);
    }

    @Override
    public Boolean updateBycommentId(Long commentId, Boolean like) {
        return commentsMapper.updateBycommentId(commentId, like);
    }


}
